﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;

namespace BL.Models.Account
{
    public class User
    {
        [Key]
        public virtual int ID { get; set; }

        public virtual string UserName { get; set; }

        public virtual string FirstName { get; set; }

        public virtual string LastName { get; set; }

        public virtual string EncPassword { get; set; }

        public virtual bool IsAuturize { get; set; }

        public virtual bool IsDeleted { get; set; }

        public static User GetByID(int userId)
        {
            User retVal = null;

            Context.Run((context) => retVal = context.Users.Where(u => u.ID == userId).FirstOrDefault());

            return retVal;
        }

        public static void AddNewUser(User user)
        {
            Context.Run((context) => 
            {
                context.Users.Add(user);
                context.SaveChanges();
            });
        }

        public static bool CheckIfNewUserIsValid(User user)
        {
            bool userNameExists = false;
            Context.Run((context) => userNameExists = context.Users.Where(u => u.UserName == user.UserName).Any());

            return !userNameExists;
        }

        public static User GetByUserName(string userName)
        {
            User user = null;

            Context.Run((context) => user = context.Users.Where(u => u.UserName.ToLower() == userName.ToLower()).FirstOrDefault());

            return user;

        }

        public static List<User> GetAllUnapprovedUsers(Context context)
        {
            return context.Users.Where(u => !u.IsAuturize && !u.IsDeleted).ToList();
        }

    }
}
